# -*- coding: utf-8 -*-
list1={'host':'localhost','user':'root','passwd':'sa','db':'test','table':'crm_members'}
list2={'host':'192.168.0.230','user':'root','passwd':'TPo**db','db':'cucrm'}

import os, sys, string
import MySQLdb
def co (x):
    x = x.decode('gbk').encode('UTF-8')
    return x

# 连接原数据库　
try:
    conn = MySQLdb.connect(host=list1['host'],user=list1['user'],passwd=list1['passwd'],db=list1['db'])#原数据库
except Exception, e:
    print e
    sys.exit()

# 获取cursor对象来进行操作

cursor = conn.cursor()

#查询出数据
sql = "select * from "+list1['table']#原数据表
cursor.execute(sql)
alldata = cursor.fetchall()#原数据集
cursor.close()

conn.close()#关闭连接

# 如果有数据返回，就循环输出, alldata是有个二维的列表
if alldata:
    try:
        conn1 = MySQLdb.connect(host=list2['host'],user=list2['user'],passwd=list2['passwd'],db=list2['db'])#新数据库
    except Exception, e:
        print e
        sys.exit()
    cursor1 = conn1.cursor()
    # 插入多条

    sql = "insert into crm_members(salesperson_id, interlevel,name,job_title,company,tel_h,tel_o,tel_m,email,address,regtime,projects_id,status) values (%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s, %s,%s)"
    val=[]
    for rec in alldata:
        val.append([0,0,co(rec[1]),co(rec[2]),co(rec[3]),co(rec[4]),co(rec[5]),co(rec[6]),co(rec[7]),co(rec[8]),co(rec[13]),2,0])
    try:
        cursor1.executemany(sql, val)
        #conn1.commit()
    except Exception, e:
        print e
    cursor1.close()

    conn1.close()

